﻿using System.Reflection;
using MyBasicLib.Reflection.FastReflection.Emit;

namespace MyBasicLib.Reflection
{
    public static class FastReflectionExtensions
    {
        public static object FastInvoke(this MethodInfo methodInfo, object instance, params object[] parameters)
        {
            return
                FastReflectionCaches.Get(methodInfo, p => new MethodInvoker(p)).Invoke(
                    instance,
                    parameters);
        }


        public static void FastSetValue(this PropertyInfo propertyInfo, object instance, object value)
        {
            FastReflectionCaches.Get(propertyInfo, p => new PropertyAccessor(p)).
                SetValue(instance,
                         value);
        }

        public static object FastGetValue(this PropertyInfo propertyInfo, object instance)
        {
            return
                FastReflectionCaches.Get(propertyInfo, p => new PropertyAccessor(p)).
                    GetValue(instance);
        }

        public static object FastGetValue(this FieldInfo fieldInfo, object instance)
        {
            return
                FastReflectionCaches.Get(fieldInfo, p => new FieldAccessor(p)).GetValue(
                    instance);
        }

        static System.Func<ConstructorInfo, ConstructorInvoker> g = p => new ConstructorInvoker(p);
        public static object FastInvoke(this ConstructorInfo constructorInfo, params object[] parameters)
        {

            return
                FastReflectionCaches.Get(constructorInfo, g).Invoke(
                                             parameters);
        }
    }
}